#ifndef __BSP_UART__H__
#define __BSP_UART__H__
#include <stdio.h>   //perror
#include <stdint.h>  //uint8_t
#include <fcntl.h>   // open
#include <termios.h> // termios, tcgetattr, tcsetattr, cfsetispeed
#include <unistd.h>  // close
#include <stdlib.h>  // malloc, free
#include <string.h>  // memset
#include <errno.h>   //EINVAL

#include "update.h"



#define BUFFER_SIZE 512
#define BLOCK_SIZE 1024       // 固定块大小为1024字节
#define BAUD_RATE 3000000     // 目标波特率（需与串口配置一致）
#define CALC_BASE_DELAY() (((uint64_t)BLOCK_SIZE * 8 * 1000000 + 1024*1000) / BAUD_RATE)
#define BASE_DELAY_USEC CALC_BASE_DELAY()
#define MAX_RETRIES 3           // 最大重试次数

typedef struct {
    char* name;         // 设备名称
    int fd;             // 文件描述符
    int baud_rate;      // 波特率
    int data_bits;      // 数据位
    int stop_bits;      // 停止位
    char parity;        // 校验位
    int32_t sec_ts;
    int32_t chr;
} Uart_t;

extern Uart_t* g_uart_mcu;
extern Uart_t* g_uart_laser;
extern Uart_t* g_uart_icr;
extern Uart_t* g_uart_vis;
extern Uart_t* g_uart_lens;
extern Uart_t* g_uart_usb0;

extern volatile int thread1_should_exit;
extern volatile int thread1_write_data_to_uart;
extern volatile int thread2_should_exit;
extern volatile int thread2_write_data_to_uart;
extern volatile int VIDEO_SELECT;
//全局变量fd，控制设备的
extern int *mcu_fd;
extern int *laser_fd;
extern int *vis_fd;
extern int *icr_fd;
extern int *icrlens_fd; 


typedef enum {
    DEVICE_LASER,
    DEVICE_VIS,
    DEVICE_ICR,
    DEVICE_UNKNOWN
} DeviceType;

typedef void (*ResponseParser)(const unsigned char*, int);
typedef struct {
    const char      *name;
    ResponseParser  parser;  // 每个设备独有的解析函数
} DeviceHandler;

//串口固定帧流水方案
#define FRAME_DATA_SIZE 4088    // 固定数据区大小(字节)
#define HEADER_SIZE     2       // 帧头大小
#define FOOTER_SIZE     2       // 帧尾大小
#define FRAME_ID_SIZE   2       // 帧号大小
#define FRAME_LEN_SIZE  2       // 帧长大小
//1 发送帧结构
typedef struct 
{
    uint8_t header[HEADER_SIZE];    // 帧头: EB 90
    uint16_t frame_id;              // 帧号(小端)
    uint16_t frame_length;          // 帧长(小端)
    uint8_t data[FRAME_DATA_SIZE];  // 数据区
    uint8_t footer[FOOTER_SIZE];    // 帧尾: AA 55
}SendFrame;


#ifdef __cplusplus
extern "C" {
#endif
void parse_laser_response(const unsigned char* response, int bytes_read);
int SendCmdToSubdev(const char* name, int fd, unsigned char* cmd, int len, int timeout);

void uart_show_data(Uart_t *ctx);
Uart_t* uart_init_context(const char* name, int baudcfg, int datacfg, int stopcfg, int paritycfg);
void uart_free_context(Uart_t* ctx);
int open_and_configure_uart(Uart_t *ctx, int _flags);

int receive_data(Uart_t* ctx, unsigned char* buf, int max_len, int timeout_ms);
int send_data(Uart_t* ctx, unsigned char* data, int len);

void uart_deinit(Uart_t **ctx_ptr) ; 
int uart_init(Uart_t *ctx, int _flags);

int uart_send_h265_frame(int fd, const void *data, size_t data_size); 
int uart_send_video_frame(int fd, const void *frame_data, size_t data_length, uint16_t frame_id);
int uart_recv_ack_frame(int fd, uint16_t *frame_id);
void uart_to_send_multidata(int fd, void *data, size_t data_size); 

void UartGetMcuData(int fd, uint8_t *buf, int len);
void uart_vis_init(void);

#ifdef __cplusplus
}
#endif
#endif 